package com.ad.pet.app.shopcar;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.cart.EditNum;
import com.ad.pet.models.remote.cart.Good;
import com.ad.pet.models.remote.cart.NewProduct;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/4/28.
 */

public class ShopingCarPresenter extends AbsBasePresenter implements ShoppingCarContract.Presenter{

    private ApiService mApiService;
    private ShoppingCarContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public ShopingCarPresenter(ApiService apiService,
                               Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(ShoppingCarContract.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }


    @Override
    public void getCartList() {
        mRxDisposable.add(mApiService.cart_lists()
                .subscribe(new AppSubscriber<CommList<List<Good>>>(mView) {
                    @Override
                    public void next(CommList<List<Good>> list) {
                        mView.getCartListSuccess(list);
                    }
                }));
    }

    @Override
    public void editCart(String cartId, String num,String sku) {
        mRxDisposable.add(mApiService.edit_item(cartId, num, sku)
                .subscribe(new AppSubscriber<EditNum>(mView) {
                    @Override
                    public void next(EditNum user) {
                        mView.editCartSuccess(user);
                    }
                }));
    }

    @Override
    public void delCart(String cartIds) {
        mRxDisposable.add(mApiService.del_item(cartIds)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.delCartSuccess();
                    }
                }));
    }


    @Override
    public void getCartPrice(String cids) {
        mRxDisposable.add(mApiService.getCartPrice(cids)
                .subscribe(new AppSubscriber<EditNum>(mView) {
                    @Override
                    public void next(EditNum user) {

                        mView.getCartPriceSuccess(user);


                    }
                }));
    }

    @Override
    public void showCartNumber() {
        mRxDisposable.add(mApiService.getUserCartProductNum()
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {

                        mView.showCartNumberSuccess(user);


                    }
                }));
    }

    @Override
    public void changeChoseStauts(String cartIds) {
        mRxDisposable.add(mApiService.updateSelectType(cartIds)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {

                        mView.changeChoseStudentsSuccess();


                    }
                }));
    }

    @Override
    public void newGetCartProduct(String cartIds) {
        mRxDisposable.add(mApiService.newGetCartProduct(cartIds)
                .subscribe(new AppSubscriber<NewProduct>(mView) {
                    @Override
                    public void next(NewProduct user) {

                        mView.newGetCartProductSuccess(user);


                    }
                }));
    }


}
